// packageA/pages/lottery/lottery_detail/lottery_detail.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    islottery: false,
    lotteryId: 0,
    lotteryResult: '',
    lotteryTime: '未参与',
    isRedeem: '',
    isOverdue: false, //是否过期
    recordList: {},
    //中奖项
    award: 1,
    mode: 2, // 旋转模式
    awardList: [{
        title: '一等奖'
      },
      {
        title: '谢谢参与'
      },
      {
        title: '二等奖'
      },
      {
        title: '谢谢参与'
      },
      {
        title: '三等奖'
      },
      {
        title: '谢谢参与'
      }
    ] // 顺时针对应每个奖项
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: app.globalData.appTitle,
    })
    let than = this
    this.setData({
      lotteryId: options.id,
      isOverdue: options.isOverdue
    })
    console.error('id ' + this.data.lotteryId)
    //请求是否参与过抽奖
    this.getVerify()
    //请求中奖列表
    this.getRecordList()
  },
  /**
   * 请求是否参与过抽奖
   */
  getVerify: function () {
    let than = this
    app.wxGetRequest('/lottery/verify', {
      lottery_id: than.data.lotteryId,
      creator_id: app.globalData.userInfo.user_id
    }).then(res => {
      than.setData({
        islottery: res.data.verify,
        lotteryResult: res.data.result,
        lotteryTime: res.data.create_time,
        isRedeem: res.data.get_time
      })
    })

  },
  //请求中奖列表
  getRecordList: function () {
    let than = this
    app.wxGetRequest("/lottery/recordList", {
      lottery_id: than.data.lotteryId
    }).then(res => {
      than.setData({
        recordList: res.data
      })
    });
  },
  // 用户点击开始抽奖
  wheelStart() {
    let than = this
    //判断是否可以抽奖
    if(than.data.isOverdue){
      wx.showToast({
        title: "该抽奖活动已结束",
        icon: 'none',
        duration: 2000
      }); 
      return
    }
    if (than.data.islottery) {
      // 设置奖项

      app.wxGetRequest("/lottery/start", {
        lottery_id: than.data.lotteryId,
        creator_id: app.globalData.userInfo.user_id,
        creator_name: app.globalData.userInfo.name
      }).then(res => {
        console.error('KKKKKKKKKKKKKKKKKK' + JSON.stringify(res))
        let level = res.data.award_level
        var award = 2
        if (level != null) {
          award = parseInt(level) * 2 - 1
        }
        than.setData({
          award: award
        })
        // 触发组件开始方法
        than.selectComponent('#sol-wheel').begin()
      })
    } else {
      setTimeout(function () {
        wx.showToast({
          title: "今日已参与过抽奖或抽奖已结束\n无法参与",
          icon: 'none',
          duration: 2000
        });
      }, 500);
    }

  },
  // 抽奖完成后操作
  wheelSuccess() {
    const data = this.data
    const index = this.data.award - 1
    console.error('KKKKKKKKKKKKKKKKKKKKKKKKKKK' + index)
    console.log('bind:success', data.awardList[index])
    setTimeout(function () {
      wx.showToast({
        title: data.awardList[index].title,
        icon: 'none',
        duration: 3000
      });
    }, 500);

    this.getRecordList()
    this.getVerify()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})